<template>
	<view>
		<uni-nav-bar :fixed="true" left-icon="back" color="#156DAC" @clickLeft="pageback">
			<view class="header-icon-text-panel">
				<text class="title">排口因子数据</text>
				<view style="margin-left: 10upx;" @click="showDrawer">
					<uni-icons type="search" size="16" color="#156DAC"></uni-icons>
				</view>
			</view>
		</uni-nav-bar>
		<view class="content padding">
			<view class="page-content-list">
				<block v-for="(value, index) in listData" :key="index">
					<view class="content-list-cell"  >
						<view class="order-item">
							<view class="order-item-content">
								<view class="order-content-row">
									<view class="label">排口名称</view><view class="value" style="color: #299AEB;">{{value.alarmInformationObj && value.alarmInformationObj.outletName}}</view>
								</view>
								<view class="order-content-row">
									<!-- style="color: #E72B2B;" -->
									<view class="label">报警类型</view><view class="value" >{{value.alarmInformationObj && value.alarmInformationObj.alarmType}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">报警信息</view><view class="value text-left">{{value.alarmInformation}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">报警开始时间</view><view class="value">{{value.alarmStartTime}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">报警结束时间</view><view class="value">{{value.alarmEndTime || '- -'}}</view>
								</view>
								<!-- <view class="order-content-row">
									<view class="label">处理人</view><view class="value">{{value.createdBy || '- -'}}</view>
								</view> -->
								<view class="order-content-row">
									<view class="label">处理内容</view><view class="value">{{value.processingResults}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">操作</view>
									<view class="value">
										<!-- 状态 -->
										<button class="order-btn" @click="goDetail(value)">因子</button>
									</view>
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>
			<uni-load-more :status="status" :icon-size="16" :content-text="contentText" />
		</view>
		
		<uniDrawer ref="draw" :mask="true" mode='right' :width="250">
			<view style="height: 90upx;">&nbsp;</view>
			<view class="draw-panel">
				<view class="operation-panel" style="padding: 0px 20upx;">
					<view class="drawer-form">
						<view class="drawer-form-item">
							<view class="drawer-label"><text>报警时长</text></view>
							<view class="drawer-content">
								<uni-easyinput class="ipt-ctl" type="number" v-model="query.duration" maxlength="8" placeholder="请输入报警时长" />
							</view>
						</view>
						<view class="drawer-form-item">
							<view class="drawer-label"><text>开始时间</text></view>
							<view class="drawer-content">
								<TimeSelector
									ref="stime"
									class="ipt-ctl"
								    showType="dateToTime"
									@btnConfirm="(e)=>dateChange(e,'alarmStartTime')" 
								>
								    <text v-if="query.alarmStartTime">{{query.alarmStartTime}}</text>
									<text v-else style="color: grey;">请选择开始时间</text>
								</TimeSelector>
							</view>
						</view>
						<view class="drawer-form-item">
							<view class="drawer-label"><text>结束时间</text></view>
							<view class="drawer-content">
								<TimeSelector
									ref="etime"
									class="ipt-ctl"
								    showType="dateToTime"
									@btnConfirm="(e)=>dateChange(e,'alarmEndTime')" 
								>
								    <text v-if="query.alarmEndTime">{{query.alarmEndTime}}</text>
									<text v-else style="color: grey;">请选择结束时间</text>
								</TimeSelector>
							</view>
						</view>
					</view>
					
					<view class="item-card">
						<view class="card-title">
							<uni-icons class="icon left" type="bars" size="20" color="#156dac"></uni-icons>
							<text class="content">报警类型</text>
							<uni-icons class="icon right" type="arrowdown" size="16" color="#666"></uni-icons>
						</view>
						<view class="operation-panel flex start" style="padding: 20upx 0 0  0;">
							<view 
								v-for="(item,index) in alarmTypes" 
								class="item-btn mini auto mr-20" 
								:class="query.alarmType === (item.code)?'':'white'" 
								@click="changeDictQuery('alarmType',item)" 
								:key="index"
							>
								<text>{{item.codeName}}</text>
							</view>
						</view>
					</view>
					
					<view class="item-card">
						<view class="card-title">
							<uni-icons class="icon left" type="paperclip" size="20" color="#156dac"></uni-icons>
							<text class="content">是否解除</text>
							<uni-icons class="icon right" type="arrowdown" size="16" color="#666"></uni-icons>
						</view>
						<view class="operation-panel flex" style="padding: 20upx 0 0  0;">
							<view 
								v-for="(item,index) in removes" 
								class="item-btn mini auto" 
								:class="query.remove === (item.code)?'':'white'" 
								@click="changeDictQuery('remove',item)" 
								:key="index"
							>
								<text>{{item.codeName}}</text>
							</view>
						</view>
					</view>
					<view class="item-card">
						<view class="card-title">
							<uni-icons class="icon left" type="flag" size="20" color="#156dac"></uni-icons>
							<text class="content">处理状态</text>
							<uni-icons class="icon right" type="arrowdown" size="16" color="#666"></uni-icons>
						</view>
						<view class="operation-panel flex" style="padding: 20upx 0 0  0;">
							<view 
								v-for="(item,index) in processingStatuss" 
								class="item-btn mini auto" 
								:class="query.processingStatus === (item.code)?'':'white'" 
								@click="changeDictQuery('processingStatus',item)" 
								:key="index"
							>
								<text>{{item.codeName}}</text>
							</view>
						</view>
					</view>
					
					<view class="item-card">
						<view class="card-title">
							<uni-icons class="icon left" type="gear" size="20" color="#156dac"></uni-icons>
							<text class="content">是否需要处理</text>
							<uni-icons class="icon right" type="arrowdown" size="16" color="#666"></uni-icons>
						</view>
						<view class="operation-panel flex" style="padding: 20upx 0 0  0;">
							<view 
								v-for="(item,index) in processs" 
								class="item-btn mini auto" 
								:class="query.process === (item.code)?'':'white'" 
								@click="changeDictQuery('process',item)" 
								:key="index"
							>
								<text>{{item.codeName}}</text>
							</view>
						</view>
					</view>
						
				</view>
				<view class="bottom-panel">
					<view class="item-btn lp" @click="handleQuery"><text>确定</text></view>
				</view>
			</view>
		</uniDrawer>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	import uniDrawer from "@/components/uni-drawer/uni-drawer.vue"
	import tkiTree from "@/components/tki-tree/tki-tree.vue"
	import LbPicker from '@/components/lb-picker'
	import TimeSelector from '@/components/wing-time-selector/wing-time-selector.vue';
	import {getDict} from '@/common/dict.js'
	import {parseTime,getRolePermiss} from '@/common/util.js'
	
	const listQuery = '/business/alarm/getAllAlarm'
	const listQueryHistroy = '/business/historyAlarm/getAllAlarm'
	const sureById = '/business/alarm/updateById'
	
	const getTree = '/base/outlet/listTree'
	export default {
		components:{
			uniIcons,
			TimeSelector,
			uniLoadMore,
			uniDrawer,
			tkiTree,
			LbPicker
		},
		data() {
			return {
				reload: false,
				listData: [],
				range: [],
				rangeKey: 'name',
				last_id: '',
				status: 'loading',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				dept: '',
				companyName: '',
				query: {
					total: 0,
					page: 1,
					limit: 10,
					outletIds: '',
					outletName: '',
					alarmType: '',
					remove: '',
					process: '',
					processingStatus: '',
					alarmStartTime: null,
					alarmEndTime: null
				},
				pickerDictProps: {
					label: 'codeName',
					value: 'code',
				},
				// 字典
				alarmTypes: [],
				removes: [],
				processingStatuss: [],
				processs: [],
				dataFlag: 1
				
			}
		},
		onLoad(options) {
			this.query.outletIds = options.outletId
			// 字典
			this.alarmTypes = [{code:'',codeName:'全部'},...getDict('e001')]
			this.removes = [{code:'',codeName:'全部'},...getDict('e002')]
			this.processingStatuss = [{code:'',codeName:'全部'},...getDict('e003')]
			this.processs = [{code:'',codeName:'全部'},...getDict('e004')]
			// 默认
			const time = parseTime(new Date(),'{y}-{m}-{d}')
			this.query.alarmStartTime = time + ' ' + '00:00:00'
			this.query.alarmEndTime =  time + ' ' + '23:59:59'
			this.getList();
			this.getTreeData();
		},
		onShow(){
			this.getList();
		},
		onPullDownRefresh() {
			this.last_id = '';
			this.listData =  []
			this.query.page = 1
			this.getList();
			setTimeout(()=>{
				uni.stopPullDownRefresh();
			},100)
		},
		onReachBottom() {
			this.status = 'more';
			this.query.page = this.query.page + 1
			this.getList(true);
		},
		methods: {
			pageback(){
				uni.navigateBack({
					delta: 1
				});
			},
			handleQuery(){
				this.query.page = 1
				this.getList();
				this.$refs.draw.close()
			},
			dateChange(res,type){
				this.query[type] = res.key
			},
			showDrawer(){
				this.$refs.draw.open()
			},
			showPkPicker() {
				this.$refs.tkitree._show()
			},
			pkConfirm(e) {
				if(e.length > 0){
					const {id,name} = e[0]
					this.query.outletIds = id
					this.query.outletName = name
				}
			},
			changeDictQuery(key,item){
				const {code} = item
				this.query[key] = code
			},
			getTreeData() {
				this.$api.request(getTree,{
					method: 'get',
				}).then(res=>{
					if(res.success){
						// 过滤无排口数据
						const result = []
						res.data.list.map(item=>{
							if(item.children && item.children.length > 0){
								result.push(item)
							}
						})
						this.range = result
					}
				}).catch(()=>{
				})
			},
			goDetail(item){
				const {factorLibraryId,alarmInformationObj,alarmStartTime,alarmEndTime} = item
				if(factorLibraryId){
					let name = '';
					if(alarmInformationObj) {
						name = alarmInformationObj.alarmFactor
					}
					// if(alarmStartTime && alarmEndTime){
					// 	times = [
					// 		parseTime(alarmStartTime, '{y}-{m}-{d} {h}:{i}:{s}'),
					// 		parseTime(alarmEndTime, '{y}-{m}-{d} {h}:{i}:{s}')
					// 	]
					// }
					const stime = alarmStartTime || ''
					const etime = alarmEndTime || ''
					uni.navigateTo({
						url:'/pages/factorMessage/factorMessage'+
						`?factorLibraryId=${factorLibraryId}&name=${name}&stime=${stime}&etime=${etime}&factorId=${factorLibraryId}&outletId=${this.query.outletIds}`,
					})
				}
			},
			getList(loadMore = false){
				this.status = 'loading';
				let c_url = listQuery
				if(this.dataFlag===2){
					c_url = listQueryHistroy
				}
				this.$api.request(c_url,{
					method: 'get',
					data: this.query
				}).then(res=>{
					if(res.success) {
						const data = res.data
						const {total,rows} = data
						if(rows.length === 0 || total <= this.query.limit){
							this.status = ''
						}
						if(loadMore){
							this.listData = [...this.listData,...rows]
						}else{
							this.listData  = rows
						}
						this.last_id = this.listData[this.listData.length - 1];
						this.reload = false;
					}
				}).catch(()=>{
				})
			},
			handleSure(item){
				uni.showModal({
					title: '提示',
					content: '是否标记已确认？',
					success: (e)=>{
						if(e.confirm){
							const {id} = item
							uni.showLoading({
								title:'确认中',mask: true
							})
							this.$api.request(sureById,{
								method: 'post',
								data: {
									id: id,
									processingStatus: 'e003_02',
								}
							}).then(res=>{
								uni.hideLoading()
								if(res.success){
									this.$api.toastMsg('已确认','success')
									this.getList()
									// setTimeout(()=> {
									// 	this.pageback()
									// }, 1000);
								}else{
									this.$api.toastMsg(res.message)
								}
							}).catch(()=>{
								uni.hideLoading()
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	
	.page-content-list{
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
		.content-list-cell{
			position: relative;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}
	}
	.order-item{
		width: 750upx;
		background: #fff;
		padding: 20upx;
		border-radius: 20upx;
		margin-bottom: 20upx;
		.order-item-content{
			margin-top: 30upx;
			.order-content-row{
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				font-size: 26upx;
				margin-bottom: 30upx;
				.order-btn{
					display: inline-block;
					background-color: #156dac;
					color: #fff;
					width: 95upx;
					height: 50upx;
					line-height: 50upx;
					text-align: center;
					font-size: 24upx;
					padding: 0;
					margin-left: 10upx;
				}
				.label{
					flex: 0 0 200upx;
					color: #5A5A5A;
					text-align: left;
					font-size: 26upx;
				}
				.value{
					flex: 1;
					color: #000;
					text-align: right;
					font-size: 26upx;
					&.text-left{
						text-align: left;
						padding-left: 20upx;
					}
				}
			}
			
		}
			
		.order-item-header{
			display: flex;
			align-items: center;
			.icon{
				margin-right: 10upx;
			}
			text{
				color: #333;
				font-size: 26upx;
				&.green{
					color:#2AD51D;
				}
				&.red{
					color: #E72B2B
				}
			}
			
		}
	}
</style>
